package com.example.gitmadhack13;

import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Element;

public class MajorCourses {
	
	private static Document doc = null; 
	private static Elements ele = null;
	private String url1; 
	private LinkedList<Course> majorRequirements = new LinkedList<Course>();
	private Major major;
	
	
	public MajorCourses(String major, String url){
		this.url1 = url;
	}
	public MajorCourses(String url){
		url1 = url;
	}
	public MajorCourses(Major major){
		this.major = major;
		getMajorURL();
	}
	
	
	
	
	
	private void getMajorURL(){
		String ac = major.getAccronym(); 
		String fn = major.getFullName();
		
		if(ac == Accronyms.BC)
			url1 = "http://www.catalog.gatech.edu/colleges/coa/arch/ugrad/bsarch/bsarch.php";
		if(ac == Accronyms.ARCH)
			url1 = "http://www.catalog.gatech.edu/colleges/coa/bc/ugrad/bsbc/bsbc.php";
		if(ac == Accronyms.ID)
			url1 = "http://www.catalog.gatech.edu/colleges/coa/id/ugrad/bsid/bsid.php";
		if(ac == Accronyms.MUSI)
			url1 = null;
		if(ac == Accronyms.MGT)
			url1 = "http://www.catalog.gatech.edu/colleges/cob/ugrad/bsba-acct.php";
		if(ac == Accronyms.CS)
			url1 = "http://www.catalog.gatech.edu/colleges/coc/ugrad/comsci/threads/degreq/info-internet-platforms.php";
		if(ac == Accronyms.AE)
			url1 = "http://www.catalog.gatech.edu/colleges/coe/ae/ugrad/bsae/bsae.php";
		if(ac == Accronyms.CHBE)
			url1 = "http://www.catalog.gatech.edu/colleges/coe/chbe/ugrad/bsche/bsche.php";
		if(ac == Accronyms.CEE)
			url1 = "http://www.catalog.gatech.edu/colleges/coe/ce/ugrad/bsce/bsce.php";
		if(ac == Accronyms.ECE)
			url1 = "http://www.catalog.gatech.edu/colleges/coe/ece/ugrad/bscmpe/bscmpe.php";
		if(ac == Accronyms.IE)
			url1 = "http://www.catalog.gatech.edu/colleges/coe/isye/ugrad/bsie/bsie-efs.php";
		if(ac == Accronyms.MSE)
			url1 = "http://www.catalog.gatech.edu/colleges/coe/mse/ugrad/bsmse/bsmse-b.php";
		if(ac == Accronyms.ME)
			url1 = "http://www.catalog.gatech.edu/colleges/coe/me/ugrad/bsme/bsme.php";
		if(ac == Accronyms.NRE)
			url1 = "http://www.catalog.gatech.edu/colleges/coe/me/ugrad/bsnre/bsnre.php";
		if(ac == Accronyms.BME)
			url1 = "http://www.catalog.gatech.edu/colleges/coe/bmed/ugrad/bsbmed/bsbmed.php";
		if(ac == Accronyms.ECON)
			url1 = "http://www.catalog.gatech.edu/colleges/cola/econ/ugrad/bs/bs.php";
		if(ac == Accronyms.CHIN)
			url1 = "http://www.catalog.gatech.edu/colleges/cola/econ/ugrad/bseconml/bsgeml-chin.php";
		if(ac == Accronyms.FREN)
			url1 = "http://www.catalog.gatech.edu/colleges/cola/econ/ugrad/bseconml/bsgeml-fren.php";
		if(ac == Accronyms.GRMN)
			url1 = "http://www.catalog.gatech.edu/colleges/cola/econ/ugrad/bseconml/bsgeml-german.php";
		if(ac == Accronyms.JAPN)
			url1 = "http://www.catalog.gatech.edu/colleges/cola/econ/ugrad/bseconml/bsgeml-japanese.php";
		if(ac == Accronyms.RUSS)
			url1 = "http://www.catalog.gatech.edu/colleges/cola/econ/ugrad/bseconml/bsgeml-russian.php";
		if(ac == Accronyms.SPAN)
			url1 = "http://www.catalog.gatech.edu/colleges/cola/econ/ugrad/bseconml/bsgeml-spanish.php";
		if(ac == Accronyms.PUBP)
			url1 = "http://www.catalog.gatech.edu/colleges/cola/pubp/ugrad/bspubp/bspubp.php";
		if(ac == Accronyms.BIO)
			url1 = "http://www.catalog.gatech.edu/colleges/cos/biology/ugrad/biology/bsbio.php";
		if(ac == Accronyms.CHEM)
			url1 = "http://www.catalog.gatech.edu/colleges/cos/chembiochem/ugrad/chem/bschem.php";
		if(ac == Accronyms.EAS)
			url1 = "http://www.catalog.gatech.edu/colleges/cos/eas/ugrad/eas/bseas.php";
		if(ac == Accronyms.MATH)
			url1 = "http://www.catalog.gatech.edu/colleges/cos/math/ugrad/mathdis/bsmathdis.php";
		if(ac == Accronyms.PHYS)
			url1 = "http://www.catalog.gatech.edu/colleges/cos/physics/ugrad/phy/bsphys.php";
		if(ac == Accronyms.PSYC)
			url1 = "http://www.catalog.gatech.edu/colleges/cos/psyc/ugrad/bspsyc/bspsyc.php";
		
	}
	public LinkedList<Course> findMajorRequirements(){
		
		try{
			doc = Jsoup.connect(url1).get();
		}
		catch(IOException ex){
			System.out.print("Website could not be found.");
		}
		
		Elements ele = doc.select("tr");
		Elements tle = doc.select("TITLE");
		String title = tle.toString().replace("<title>", "").replace("</title", "");
		String[] str = title.split(": ");
		
		for(int ndx = 0; ndx < ele.size(); ndx++){
			String subject = null; int courseName = 0;
			Elements e = ele.get(ndx).select("td");
			if(e.size() <= 2)
				continue;
			Element hold = e.subList(2, 3).get(0);
			String course = hold.toString().replace("<td>", "").replace("</td>", "");
			String[] arr = course.split(" ");
			if(arr.length == 2){
				subject = arr[0];
				if(isInteger(arr[1])){
					courseName = Integer.parseInt(arr[1]);
				}
			}
			majorRequirements.add(new Course(subject, courseName));
		}
		
		for(int ndx = 0; ndx < majorRequirements.size(); ndx++){
			if(majorRequirements.get(ndx).getCourseNumber() == 0 || majorRequirements.get(ndx).getSubject() == null){
				majorRequirements.remove(ndx);
			}
		}
		
		majorRequirements.removeLast();
		return majorRequirements;
	}
	
	public boolean isInteger(String input){
		try{
			Integer.parseInt(input);
			return true;
		}catch(Exception e){
			return false;
		}	
	}
}

